package com.ybwh.springboot2.sqlparse;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.util.JdbcConstants;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author fanbeibei
 * @version 1.0.0
 * @date 2023/6/14 11:59
 */
public class SqlParser {


    public static void printParamIndex(String sql, List<Integer> params) {
        List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
        System.out.println("-------------------------------------");
        for (SQLStatement stmt : stmtList) {
            stmt.accept(new LikeParamIndexesVisitor(params));
        }


        System.out.println("-------------------------------------");
        String result = SQLUtils.toSQLString(stmtList, JdbcConstants.MYSQL);

        System.out.println(result);
    }


    public static void main(String[] args) {


        List<Integer> params = Lists.newArrayList(1, 2, 3);
        String sql = "select * from  tt where t.name like concat('%',?,?,'%') and t.nick like ?";
//        printParamIndex(sql, params);
//
//
//        params = Lists.newArrayList(1, 2, 3, 4, 5, 6);
//        sql = " select * from  order where o_no = ?  and good_name like  concat('%',?,?,'%') and  (select id from  tt where t.name like concat('%',?,?,'%') and t.nick like ?)";
//        printParamIndex(sql, params);



        params = Lists.newArrayList(1, 2, 3, 4, 5, 6);
        sql = " select * from  (select * from  goods t where t.name like concat('%',?,?,'%') and t.nick like ?) tt  where tt.o_no = ?  and tt.good_name like  concat('%',?,?,'%')";
        printParamIndex(sql, params);

    }


}
